package ru.javadevelop.vegetarian.fragments;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import ru.javadevelop.vegetarian.R;
import ru.javadevelop.vegetarian.activities.RecipeDetailsActivity;
import ru.javadevelop.vegetarian.activities.RecipesInCatActivity;
import ru.javadevelop.vegetarian.adapters.CategoriesDataAdapter;
import ru.javadevelop.vegetarian.adapters.RecipesDataAdapter;
import ru.javadevelop.vegetarian.common.ExtApplication;
import ru.javadevelop.vegetarian.common.interfaces.IRefreshable;
import ru.javadevelop.vegetarian.model.Category;
import ru.javadevelop.vegetarian.model.Recipe;

import java.util.ArrayList;

public class RecipesFragment extends Fragment {

    private View view;
    private Context _context;
    private GridView grid_view;
    private RecipesDataAdapter adapter;
    private ArrayList<Recipe> recipes;
    //******************************************************************************************************************
    public RecipesFragment(ArrayList<Recipe> recipes)
    {
        this.recipes = recipes;
    }
    //******************************************************************************************************************
    public RecipesFragment()
    {
        this.recipes = Recipe.getFavorites();
    }
    //******************************************************************************************************************
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        view = inflater.inflate(R.layout.fragment_recipes, container, false);

        findControls();
        setListeners();
        fillControls();
        return view;
    }
    //******************************************************************************************************************
    @Override
    public void  onResume()
    {
        super.onResume();
        fillControls();
    }
    //******************************************************************************************************************
    @Override
    public void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        _context = getActivity();

    }

    //******************************************************************************************************************
    public void findControls()
    {
        grid_view = (GridView)view.findViewById(R.id.grid_view);
    }
    //******************************************************************************************************************
    public void fillControls()
    {
        if (recipes!=null)
        {
            adapter = new RecipesDataAdapter(_context, R.layout.items_recipe, recipes);
            grid_view.setAdapter(adapter);
        }
    }
    //******************************************************************************************************************
    private void setListeners()
    {
        grid_view.setOnItemSelectedListener(new GridItemSelectedLstnr());
        grid_view.setOnItemClickListener(new GridItemClickLstnr());

    }
    //******************************************************************************************************************
    private class GridItemSelectedLstnr implements AdapterView.OnItemSelectedListener
    {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            //To change body of implemented methods use File | Settings | File Templates.
        }
    }
    //******************************************************************************************************************
    private class GridItemClickLstnr implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ExtApplication.Session.recipe = adapter.getItem(position);
            try {
                ExtApplication.Session.category = ExtApplication.Session.recipe.getCategory();
                Intent i = new Intent(_context, RecipeDetailsActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                startActivity(i);
            }
            catch (Exception e)
            {
                RecipesFragment.this.fillControls();
                try {
                    _context.getClass().getMethod("refresh");
                    ((IRefreshable)_context).refresh();
                } catch (NoSuchMethodException ignored) {

                }
            }
        }
    }

    //******************************************************************************************************************
}
